package main;

import java.util.*;
import fmdao.*;
import dbdao.*;

public class InsertPrecios 
{
	private ArrayList<String> lS;
	private ArrayList<String> lT;
	private ArrayList<String> lSQL;
	
	public InsertPrecios()
	{
		lSQL = new ArrayList<String>();
		lS = new ArrayList<String>();
		lT = new ArrayList<String>();
		lS.add("micro.txt");
		lT.add("micro");
		lS.add("frigo.txt");
		lT.add("frigo");
		lS.add("lavadoras.txt");
		lT.add("lavadoras");
		lS.add("lavavajillas.txt");
		lT.add("lavavajillas");
		insertPreciosOrdenIndex();
		
		/*
		lS = new ArrayList<String>();
		lT = new ArrayList<String>();
		lS.add("tv.txt");
		lT.add("tv");
		insertPreciosMarcaModelo();
		*/
		//seeSQL();
		update();
	}
	
	private void insertPreciosOrdenIndex()
	{
		for( int i=0 ; i<lS.size(); i++ )
		{
			FileManager fm = new FileManager( lS.get(i) );
			String str = fm.readLine();
			int j=0;
			while( str!=null )
			{
				String sql = "UPDATE " + lT.get(i) + " SET precio=" + str + " WHERE id=" + j;
				lSQL.add(sql);
				str = fm.readLine();
				j++;
			}
		}
	}
	
	private void insertPreciosMarcaModelo()
	{
		for( int i=0 ; i<lS.size(); i++ )
		{
			FileManager fm = new FileManager( lS.get(i) );
			String str = fm.readLine();
			int j=0;
			while( str!=null )
			{
				String marca = str.substring(0,str.indexOf("-"));
				String modelo = str.substring(str.indexOf("-")+1,str.indexOf("-",str.indexOf("-")+1));
				String precio = str.substring(str.indexOf("-",str.indexOf("-")+1)+1);
				String sql = "UPDATE " + lT.get(i) + " SET precio = " + precio + " WHERE marca='" + marca + "' AND modelo='" + modelo + "'";
				lSQL.add(sql);
				str = fm.readLine();
				j++;
			}
		}
	}
	
	private void seeSQL()
	{
		for( int i=0 ; i<lSQL.size() ; i++ )
			System.out.println(lSQL.get(i));
	}
	
	private void update()
	{
		DBaseManager dbm = new DBaseManager();
		dbm.updateElements(lSQL);
	}
}
